package com.gosophia.conference.action.scheme;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.conferenceManagement.common.dto.DocumentDTO;
import com.gosophia.conferenceManagement.scheme.dto.SchemeTransientStoreDTO;
import com.gosophia.conferenceManagement.scheme.ws.ConferenceSchemeWebService;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.taskManagement.dto.JobDTO;
import com.gosophia.taskManagement.ws.TaskManagementWebService;
import com.gosophia.userManagement.dto.UserInfoDTO;
import com.opensymphony.xwork2.ActionContext;

/**
 * 保存或更新action：保存、编辑及自动保存均使用该action
 * @author mayue
 * @since 2011-6-2
 */
@Scope("prototype")
@Controller("SaveOrUpdateAction")
public class SaveOrUpdateAction extends BaseSupportAction{

    private static final long serialVersionUID = 1L;

    @Autowired
    private ConferenceSchemeWebService  conferenceSchemeWebService;
    @Autowired
	private TaskManagementWebService taskManagementWebService;
    private SchemeTransientStoreDTO schemeTransientStoreDTO;
    private Long jobId;
   
	private String onlinePath;
    
    private String onlineVersion;
    @Override
    public String _execute() throws Exception {
        
        UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
        .getContext().getAuthentication().getPrincipal();
        UserInfoDTO creatorDTO = userDetails.getUserAccountDTO().getUserInfoDTO();
        if(schemeTransientStoreDTO.getCreatorDTO()==null){
            schemeTransientStoreDTO.setCreatorDTO(creatorDTO);
        }
        schemeTransientStoreDTO.setCreatDate(new Date());
        DocumentDTO schemeDocumentDTO = new DocumentDTO();
        schemeDocumentDTO.setDocumentPath(onlinePath);
        schemeDocumentDTO.setDocumentVersion(onlineVersion);
        schemeDocumentDTO.setDocumentCategory("online");
        schemeTransientStoreDTO.setSchemeDocumentDTO(schemeDocumentDTO);
        Long returnId = null ;
        if(jobId !=null){
        	 JobDTO jobDto = taskManagementWebService.findJobDetailByJobId(jobId);
        	 schemeTransientStoreDTO.setConferenceName(jobDto.getMissionDTO().getMissionName());
        	 schemeTransientStoreDTO.setPlanEndDate(jobDto.getMissionDTO().getScheduleEndDate());
        	 schemeTransientStoreDTO.setDescription(jobDto.getMissionDTO().getDescription());
        	 schemeTransientStoreDTO.setImportanceDTO(jobDto.getMissionDTO().getImportanceDTO());
        	 returnId = conferenceSchemeWebService.saveOrUpdateSchemeTransientStore(schemeTransientStoreDTO);
        }else{
        	
          returnId = conferenceSchemeWebService.saveOrUpdateSchemeTransientStore(schemeTransientStoreDTO);
        }
        if(returnId!=null){
            schemeTransientStoreDTO.setSchemeTransientStoreId(returnId);
        }
        
        super.getRequestResult().setRsltCode("0");
        super.getRequestResult().setMessage("会议方案保存成功");
        ActionContext.getContext().getSession().put("_lastOpResult", super.getRequestResult());
        if("task".equals(this.getFromFlag())){
            return "TASK";
        }
        return SUCCESS;
    }
    public SchemeTransientStoreDTO getSchemeTransientStoreDTO() {
        return schemeTransientStoreDTO;
    }
    public void setSchemeTransientStoreDTO(
            SchemeTransientStoreDTO schemeTransientStoreDTO) {
        this.schemeTransientStoreDTO = schemeTransientStoreDTO;
    }
    public String getOnlinePath() {
        return onlinePath;
    }
    public void setOnlinePath(String onlinePath) {
        this.onlinePath = onlinePath;
    }
    public String getOnlineVersion() {
        return onlineVersion;
    }
    public void setOnlineVersion(String onlineVersion) {
        this.onlineVersion = onlineVersion;
    }
    public Long getJobId() {
		return jobId;
	}
	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}
}
